﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
    public partial class FormDict : Form
    {
        Dictionary<string, Student> dictStus = new Dictionary<string, Student>();
        Dictionary<string, Student> dictStus1 = null;

        public FormDict()
        {
            InitializeComponent();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            dictStus1 = new Dictionary<string, Student>()
            {
                { "01", new Student() { Sno = "01", Name = "刘备", Birth = DateTime.Parse("2005-7-5") } },
                { "02", new Student() { Sno = "02", Name = "关羽", Birth = DateTime.Parse("2006-7-5") } },
                { "03", new Student() { Sno = "03", Name = "张飞", Birth = DateTime.Parse("2007-7-5") } },
            };
        }

        private void btnLoop_Click(object sender, EventArgs e)
        {
            foreach(KeyValuePair<string, Student> kvp in dictStus1)
            {                
                MessageBox.Show(kvp.Value.SayHello());                 
            }

            foreach(string key in dictStus1.Keys)
            {
                Student s = dictStus1[key];
                MessageBox.Show(s.SayHello());
            }

            foreach(Student s in dictStus1.Values)
            {
                MessageBox.Show(s.SayHello());
            }
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbSno.Text) )
            {
                if (dictStus1.ContainsKey(tbSno.Text))
                {
                    Student s = dictStus1[tbSno.Text];
                    if (s != null)
                    {
                        MessageBox.Show(s.SayHello());
                    }
                }
                else
                {
                    MessageBox.Show(string.Format("对不起，对应的学号{0}不存在，请重新输入！", tbSno.Text));
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Student s = new Student { Sno = "04", Name = "诸葛亮", Birth = DateTime.Parse("2001-05-09") };
            dictStus1[s.Sno] = s;

            s = new Student { Sno = "01", Name = "赵云", Birth = DateTime.Parse("2004-05-09") };
            dictStus1[s.Sno] = s;

            foreach (Student ss in dictStus1.Values)
            {
                MessageBox.Show(ss.SayHello());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Student s = new Student { Sno = "01", Name = "赵云", Birth = DateTime.Parse("2004-05-09") };
            if (!dictStus1.ContainsKey(s.Sno))
            {
                dictStus1.Add(s.Sno, s);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbSno.Text))
            {
                dictStus1.Remove(tbSno.Text);
            }

            foreach (Student ss in dictStus1.Values)
            {
                MessageBox.Show(ss.SayHello());
            }
        }

        Students ss = new Students();

        private void btnExtend_Click(object sender, EventArgs e)
        {
            Student s = new Student() { Sno = "01", Name = "刘备", Birth = DateTime.Parse("2005-7-5") };
            ss[s.Sno] = s;
            s = new Student() { Sno = "02", Name = "张飞", Birth = DateTime.Parse("2005-7-5") };
            ss[s.Sno] = s;
            s = new Student() { Sno = "03", Name = "关羽", Birth = DateTime.Parse("2005-7-5") };
            ss[s.Sno] = s;
            s = new Student() { Sno = "04", Name = "张飞", Birth = DateTime.Parse("2005-7-5") };
            ss[s.Sno] = s;
        }

        private void btnLoop1_Click(object sender, EventArgs e)
        {
            foreach(string sno in ss.Snoes)
            {
                MessageBox.Show(ss[sno].SayHello());
            }
        }

        private void btnLookUp_Click(object sender, EventArgs e)
        {
            if (ss[tbSno.Text] != null)
            {
                MessageBox.Show(ss[tbSno.Text].SayHello());
            }
        }

        private void btnLookup2_Click(object sender, EventArgs e)
        {
            foreach(Student s in ss[tbName.Text, 1])
            {
                MessageBox.Show(s.SayHello());
            }            
        }
    }

    class Student
    {
        //string sno;
        //public string Sno
        //{
        //    get
        //    {
        //        return sno;
        //    }
        //    set
        //    {
        //        sno = value;
        //    }
        //}

        public string Sno
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public DateTime Birth
        {
            get;
            set;
        }

        public string SayHello()
        {
            string fmt = "你好！我是{0}，我的学号是{1}, 我今年{2}岁";
            Double dblAge = (DateTime.Now - Birth).TotalDays / 365;
            return string.Format(fmt, Name, Sno, dblAge.ToString("F1"));
        }
    }

    class Students
    {
        Dictionary<string, Student> dictStus = new Dictionary<string, Student>();

        public Student this[string sno]
        {
            get
            {
                Student rtn = dictStus.ContainsKey(sno) ? dictStus[sno] : null;
                return rtn;
            }
            set
            {
                dictStus[sno] = value;
            }
        }

        public List<Student> this[string name, int flag]
        {
            get
            {
                List<Student> rtn = new List<Student>();
                foreach(Student s in dictStus.Values)
                {
                    if (s.Name == name)
                    {
                        rtn.Add(s);
                    }
                }
                return rtn;
            }           
        }

        public List<string> Snoes
        {
            get
            {
                return dictStus.Keys.ToList();
            }
        }
    }
}
